package Factory;

import Factory.request.RequestParams;
import io.netty.handler.codec.http.*;
import io.netty.util.CharsetUtil;

import java.net.URI;
import java.util.Map;

public class RequestFactory {

    private FullHttpRequest getDefaultPostRequest(URI uri) {
        FullHttpRequest fullHttpRequest = null;
        fullHttpRequest = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.POST, uri.toASCIIString());
        return fullHttpRequest;
    }

    private FullHttpRequest requetDecorate(FullHttpRequest fullHttpRequest) {
        fullHttpRequest.headers().addInt(HttpHeaderNames.CONTENT_LENGTH, fullHttpRequest.content().readableBytes());
        return fullHttpRequest;
    }

    public static FullHttpRequest getPostFullRequest(RequestParams requestParams) {
        if (requestParams.getHttpMethod() != HttpMethod.POST) {
            throw new RuntimeException("please use the POST request");
        }
        FullHttpRequest fullHttpRequest = getFullRequest(requestParams);
        fullHttpRequest.content().writeBytes(requestParams.getParams());
        fullHttpRequest.headers().addInt(HttpHeaderNames.CONTENT_LENGTH, fullHttpRequest.content().readableBytes());
        return fullHttpRequest;
    }

    public static FullHttpRequest getGetFullRequest(RequestParams requestParams) {
        if (requestParams.getHttpMethod() != HttpMethod.GET) {
            throw new RuntimeException("please use the POST request");
        }
        FullHttpRequest fullHttpRequest = getFullRequest(requestParams);
        fullHttpRequest.headers().addInt(HttpHeaderNames.CONTENT_LENGTH, fullHttpRequest.content().readableBytes());
        return fullHttpRequest;
    }

    private static FullHttpRequest getFullRequest(RequestParams requestParams) {
        FullHttpRequest fullHttpRequest = new DefaultFullHttpRequest(requestParams.getHttpVersion(), requestParams.getHttpMethod(), requestParams.getUri().toASCIIString());
        fullHttpRequest.headers().add(requestParams.getHeaders());
        return fullHttpRequest;
    }
}
